import time
from multiprocessing import Process, Event, connection, Pipe, Queue
from typing import Optional


def NewProcess(event, pipe: Queue):
    nums = 0
    while nums <= 20 and not event:
        pipe.put(f'运行中[{nums}]'.encode())
        nums += 1
    pipe.put(f'运行中[{nums}]'.encode())


class MyProcess(Process):
    MyType = Optional[Process]

    def __init__(self):
        super(MyProcess, self).__init__()

    def run(self) -> None:
        event = Event()
        pipe = Queue(30)
        proc = Process(target=NewProcess, args=(event, pipe))
        print("已开启子进程的子进程")
        proc.start()
        nums = 0
        while nums < 5:
            if pipe.empty():
                print("空的！")
                continue
            else:
                print("进入第一次等待")
                data: bytes = pipe.get()
                print(data.decode())
                nums += 1
        event.set()
        print("等待子进程的子进程结束")
        proc.join()


if __name__ == '__main__':
    allEvent = Event()
    proc = MyProcess()
    proc.start()
    print("已经进入子进程")
    time.sleep(5)
    print("等待子进程结束")
    proc.join()
